#include ".\dxmanager.h"

dxManager::dxManager(void)
{
	pD3D = NULL;
 	pd3dDevice = NULL;
	m_font = NULL;
	spriteDevice = NULL;
	v_font = NULL;
}

dxManager::~dxManager(void)
{
}

bool dxManager::init(HWND hwnd)
{
	if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
	{
		lastResult = E_FAIL;
		return false;
	}

	D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
	d3dpp.BackBufferCount  = 1;
	d3dpp.BackBufferHeight = 600;
	d3dpp.BackBufferWidth  = 800;
	d3dpp.hDeviceWindow    = hwnd;
	d3dpp.EnableAutoDepthStencil = TRUE;
	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

    if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &pd3dDevice ) ) )
    {
		lastResult = E_FAIL;
        return false;
    }

	lastResult = D3DXCreateFontA(pd3dDevice,
					50,
					0,
					FW_NORMAL,
					1,
					FALSE,
					DEFAULT_CHARSET,
					OUT_DEFAULT_PRECIS,
					DEFAULT_QUALITY,
					DEFAULT_PITCH | FF_DONTCARE,
					"Courier New",
					&m_font);

	if(lastResult == E_FAIL)
		return false;

	if(FAILED(D3DXCreateSprite(pd3dDevice, &spriteDevice)))
	{
		lastResult = E_FAIL;
		return false;
	}

	lastResult = D3DXCreateFontA(pd3dDevice,
					12,
					0,
					FW_NORMAL,
					1,
					FALSE,
					DEFAULT_CHARSET,
					OUT_DEFAULT_PRECIS,
					DEFAULT_QUALITY,
					DEFAULT_PITCH | FF_DONTCARE,
					"Arial Bold",
					&v_font);

	if(lastResult == E_FAIL)
		return false;

	return true;
}

void dxManager::shutdown(void)
{
    if( pd3dDevice != NULL) 
	{
        pd3dDevice->Release();
		pd3dDevice = NULL;
	}
    if( pD3D != NULL)
	{
        pD3D->Release();
		pD3D = NULL;
	}

	if( m_font != NULL)
	{
		m_font->Release();
		m_font = NULL;
	}

	if(spriteDevice != NULL)
	{
		spriteDevice->Release();
		spriteDevice = NULL;
	}

	if( v_font != NULL)
	{
		v_font->Release();
		v_font = NULL;
	}
}

void dxManager::beginRender()
{
	if( NULL == pd3dDevice )
        return;

    // Clear the backbuffer
    pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
	pd3dDevice->BeginScene();
}

void dxManager::endRender(void)
{
	pd3dDevice->EndScene();
	// Present the backbuffer contents to the display
    pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

IDirect3DSurface9* dxManager::getSurfaceFromBitmap(std::string filename)
{
	HRESULT hResult;
	IDirect3DSurface9* surface = NULL;
	D3DXIMAGE_INFO imageInfo;

	// Get the width and height info from this bitmap
	hResult = D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo);
	if FAILED (hResult)
		return NULL;


	hResult = pd3dDevice->CreateOffscreenPlainSurface(imageInfo.Width, imageInfo.Height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);
	if (FAILED(hResult))
		return NULL;

	hResult = D3DXLoadSurfaceFromFileA(surface, NULL, NULL, filename.c_str(), NULL, D3DX_DEFAULT, 0xff000000, NULL);
	if (FAILED(hResult))
		return NULL;

	return surface;
}

IDirect3DSurface9* dxManager::getBackBuffer(void)
{
	IDirect3DSurface9* backbuffer = NULL;

	if (!pd3dDevice)
		return NULL;

	HRESULT hResult = pd3dDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO, &backbuffer);
	if (FAILED(hResult))
		return NULL;
	else
		return backbuffer;
}

void dxManager::blitToSurface(IDirect3DSurface9* srcSurface, const RECT *srcRect, const RECT *destRect)
{
	pd3dDevice->StretchRect(srcSurface, srcRect, getBackBuffer(), destRect, D3DTEXF_NONE);
}

void dxManager::printText(const int& phase)
{
	RECT rct;		
	SetRect(&rct, 100, 100, 800, 600);
	char text[255] = "";

	switch(phase)
	{
	case 10:
	case 2:
		strcpy_s(text, 255, "Player 2 turn\nPress Enter to continue");
		break;
	case 4:
		strcpy_s(text, 255, "Player 1 turn to discard\nPress Enter to continue");
		break;
	case 6:
		strcpy_s(text, 255, "Player 2 turn to discard\nPress Enter to continue");
		break;
	case 8:
		strcpy_s(text, 255, "Player 1 turn\nPress Enter to continue");
		break;
	case 12:
		strcpy_s(text, 255, "Player 1 turn to swap\nPress Enter to continue");
		break;
	case 14:
		strcpy_s(text, 255, "Player 2 turn to swap\nPress Enter to continue");
		break;
	}

	//draw text
	m_font->DrawTextA(NULL, text, -1, &rct, 0, 0xffffffff);
}

void dxManager::GetTextureFromFile(char *filename, LPDIRECT3DTEXTURE9* texture)
{
	D3DXCreateTextureFromFileExA(pd3dDevice, filename, D3DX_DEFAULT_NONPOW2, D3DX_DEFAULT_NONPOW2,
		D3DX_FROM_FILE, 0, D3DFMT_FROM_FILE, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0xffffffff,
		NULL, NULL, texture);
}

void dxManager::ShowTextureOverlay(LPDIRECT3DTEXTURE9 texture, const D3DXVECTOR3& dest)
{
	spriteDevice->Begin(D3DXSPRITE_ALPHABLEND);
	spriteDevice->Draw(texture, NULL, NULL, &dest, D3DCOLOR_ARGB(255, 255, 255, 255));
	spriteDevice->End();
}

void dxManager::printColorVictoryText(const int& p1Color1, const int& p1Color2,
									  const int& p2Color1, const int& p2Color2)
{
	RECT rct, rctOne;		
	SetRect(&rct, 32, 70, 800, 600);
	SetRect(&rctOne, 32, 520, 800, 600);

	char dString[35];
	sprintf(dString," %d                       %d", p1Color1, p1Color2);

	v_font->DrawTextA(NULL, dString, -1, &rct, 0, 0xff000000);

	char fString[35];
	sprintf(fString," %d                       %d", p2Color1, p2Color2);
	v_font->DrawTextA(NULL, fString, -1, &rctOne, 0, 0xff000000);
}

void dxManager::PrintScore(int score1, int score2)
{
	RECT rct, rctOne;
	SetRect(&rct, 55, 185, 800, 600);
	SetRect(&rctOne, 55, 400, 800, 600);

	char dString[35];
	sprintf(dString,"%d", score1);
	m_font->DrawTextA(NULL, dString, -1, &rct, 0, 0xff000000);

	char fString[35];
	sprintf(fString,"%d", score2);
	m_font->DrawTextA(NULL, fString, -1, &rctOne, 0, 0xff000000);
}